package com.example.football;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.util.JDBCUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterliketeamActivity extends AppCompatActivity {
    TextView liketeam[] =new TextView[15];
    boolean i[] = new boolean[15];
    Integer peopleid;
    String account;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registerliketeam);

        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        Intent intent=getIntent();
        peopleid= intent.getIntExtra("id",0);
        account = intent.getStringExtra("account");

        liketeam[0] = findViewById(R.id.liketeam_11_tv);
        liketeam[1] = findViewById(R.id.liketeam_12_tv);
        liketeam[2] = findViewById(R.id.liketeam_13_tv);
        liketeam[3] = findViewById(R.id.liketeam_21_tv);
        liketeam[4] = findViewById(R.id.liketeam_22_tv);
        liketeam[5] = findViewById(R.id.liketeam_23_tv);
        liketeam[6] = findViewById(R.id.liketeam_31_tv);
        liketeam[7] = findViewById(R.id.liketeam_32_tv);
        liketeam[8] = findViewById(R.id.liketeam_33_tv);
        liketeam[9] = findViewById(R.id.liketeam_41_tv);
        liketeam[10] = findViewById(R.id.liketeam_42_tv);
        liketeam[11] = findViewById(R.id.liketeam_43_tv);
        liketeam[12] = findViewById(R.id.liketeam_51_tv);
        liketeam[13] = findViewById(R.id.liketeam_52_tv);
        liketeam[14] = findViewById(R.id.liketeam_53_tv);

        for(int j = 0;j<15;j++)
            i[j] = false;

    }

    public void liketeam_11_tv(View view){
        if(i[0]==false){
            i[0] = true;
            liketeam[0].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[0]==true){
            i[0] = false;
            liketeam[0].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_12_tv(View view){
        if(i[1]==false){
            i[1] = true;
            liketeam[1].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[1]==true){
            i[1] = false;
            liketeam[1].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_13_tv(View view){
        if(i[2]==false){
            i[2] = true;
            liketeam[2].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[2]==true){
            i[2] = false;
            liketeam[2].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_21_tv(View view){
        if(i[3]==false){
            i[3] = true;
            liketeam[3].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[3]==true){
            i[3] = false;
            liketeam[3].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_22_tv(View view){
        if(i[4]==false){
            i[4] = true;
            liketeam[4].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[4]==true){
            i[4] = false;
            liketeam[4].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_23_tv(View view){
        if(i[5]==false){
            i[5] = true;
            liketeam[5].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[5]==true){
            i[5] = false;
            liketeam[5].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_31_tv(View view){
        if(i[6]==false){
            i[6] = true;
            liketeam[6].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[6]==true){
            i[6] = false;
            liketeam[6].setTextColor(getResources().getColor(R.color.black));
        }
    }
    public void liketeam_32_tv(View view){
        if(i[7]==false){
            i[7] = true;
            liketeam[7].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[7]==true){
            i[7] = false;
            liketeam[7].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_33_tv(View view){
        if(i[8]==false){
            i[8] = true;
            liketeam[8].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[8]==true){
            i[8] = false;
            liketeam[8].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_41_tv(View view){
        if(i[9]==false){
            i[9] = true;
            liketeam[9].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[9]==true){
            i[9] = false;
            liketeam[9].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_42_tv(View view){
        if(i[10]==false){
            i[10] = true;
            liketeam[10].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[10]==true){
            i[10] = false;
            liketeam[10].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_43_tv(View view){
        if(i[11]==false){
            i[11] = true;
            liketeam[11].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[11]==true){
            i[11] = false;
            liketeam[11].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_51_tv(View view){
        if(i[12]==false){
            i[12] = true;
            liketeam[12].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[12]==true){
            i[12] = false;
            liketeam[12].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_52_tv(View view){
        if(i[13]==false){
            i[13] = true;
            liketeam[13].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[13]==true){
            i[13] = false;
            liketeam[13].setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void liketeam_53_tv(View view){
        if(i[14]==false){
            i[14] = true;
            liketeam[14].setTextColor(getResources().getColor(R.color.red));
        }
        else if(i[14]==true){
            i[14] = false;
            liketeam[14].setTextColor(getResources().getColor(R.color.black));
        }
    }


    public void team_insert(View view){
        EditText et = findViewById(R.id.et_zidingyi);
        String team = et.getText().toString().trim();
        if(team.equals("")){
            Toast.makeText(RegisterliketeamActivity.this, "球队名不能为空", Toast.LENGTH_SHORT).show();
        }else {
            JSONObject jsonObject = new JSONObject();
//            String peopleid = tv.getText().toString();
            try {
                jsonObject.put("peopleid", peopleid);
                jsonObject.put("liketeam", team);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String url="http://"+ JDBCUtils.ip +":9000/APPaddPeopleliketeam";
            RequestQueue requestQueue= Volley.newRequestQueue(RegisterliketeamActivity.this);
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        Log.d("添加信息", jsonObject.toString());
                        String msg = jsonObject.getString("msg");
                        Toast.makeText(RegisterliketeamActivity.this, msg, Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(RegisterliketeamActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        }

    }
    public void team_queding(View view){
        for(int j = 0; j<15;j++){
            String team = liketeam[j].getText().toString().trim();
            if(i[j]==true){
                i[j] = false;
                liketeam[j].setTextColor(getResources().getColor(R.color.black));
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("peopleid", peopleid);
                    jsonObject.put("liketeam", team);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String url="http://"+ JDBCUtils.ip +":9000/APPaddPeopleliketeam";
                RequestQueue requestQueue= Volley.newRequestQueue(RegisterliketeamActivity.this);
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            Log.d("添加信息", jsonObject.toString());
                            String msg = jsonObject.getString("msg");
                            Toast.makeText(RegisterliketeamActivity.this, msg, Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(RegisterliketeamActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(jsonObjectRequest);
            }
        }
    }

    public void team_wancheng(View view){
        Intent intent=new Intent(RegisterliketeamActivity.this,RegisterlikestarActivity.class);
        intent.putExtra("peopleid",peopleid);
        intent.putExtra("account",account);
        startActivity(intent);
    }
}
